package com.jbh.yxjdemobykontlin

import android.util.Log
import com.google.gson.Gson
import java.util.Calendar

class TimeDateUtil {
    /**
     * 实例
     */
    var calender: Calendar = Calendar.getInstance()
    fun info() {
        //年
        val year = calender.get(Calendar.YEAR)
        //月
        val month = calender.get(Calendar.MONTH) + 1
        //天
        val day = calender.get(Calendar.DAY_OF_MONTH)
        Log.d("[com.jbh.yxjdemobykontlin.TimeDateUtil]", "[time]-> = ${year}年${month}月${day}日")


        calender.add(Calendar.DAY_OF_MONTH, -7);
        //年
        val year1 = calender.get(Calendar.YEAR)
        //月
        val month1 = calender.get(Calendar.MONTH) + 1
        //天
        val day1 = calender.get(Calendar.DAY_OF_MONTH)
        Log.d("[com.jbh.yxjdemobykontlin.TimeDateUtil]", "[time]-> = ${year1}年${month1}月${day1}日")


        calender.set(Calendar.MONTH, 0)
        calender.set(Calendar.DAY_OF_MONTH, 1)

//        while (calender.get(Calendar.YEAR) == year){
//            //年
//            val year2 = calender.get(Calendar.YEAR)
//            //月
//            val month2 = calender.get(Calendar.MONTH)+1
//            //天
//            val day2 = calender.get(Calendar.DAY_OF_MONTH)
//            val ds=mutableListOf<String>();
//
//            list.add(TimeBean(year.toString(),month2.toString(),ds))
//            Log.d("[com.jbh.yxjdemobykontlin.TimeDateUtil]","[time]-> = ${year2}年${month2}月${day2}日")
//            calender.add(Calendar.DAY_OF_MONTH,1)
//        }


        val list = mutableListOf<TimeBean>()
        calender.set(Calendar.MONTH, 0)
        calender.set(Calendar.DAY_OF_MONTH, 1)
        for (i in 0..11) {
            calender.set(Calendar.MONTH, i)
            val bean = TimeBean(calender.get(Calendar.YEAR).toString(), (i).toString())
            // 获取当月的天数
            val daysInMonth: Int = calender.getActualMaximum(Calendar.DAY_OF_MONTH)
            val days = mutableListOf<String>();
            for (dd in 1..daysInMonth) {
                days.add(dd.toString())
            }
            bean.day = days;
            list.add(bean)
        }
        Log.d("[com.jbh.yxjdemobykontlin.TimeDateUtil]","[time]-> = ${Gson().toJson(list)}")
        Log.d("[TimeDateUtil]","[currentYearList]-> = ${Gson().toJson(Calendar.getInstance().currentYearList())}")
    }


    fun Calendar.currentYearList() : MutableList<TimeBean>{
        val list = mutableListOf<TimeBean>()
        set(Calendar.MONTH, 0)
        set(Calendar.DAY_OF_MONTH, 1)
        for (i in 0..11) {
            set(Calendar.MONTH, i)
            val bean = TimeBean(get(Calendar.YEAR).toString(), (i).toString())
            // 获取当月的天数
            val daysInMonth: Int = getActualMaximum(Calendar.DAY_OF_MONTH)
            val days = mutableListOf<String>();
            for (dd in 1..daysInMonth) {
                days.add(dd.toString())
            }
            bean.day = days;
            list.add(bean)
        }
        return list
    }
}